<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/4/7
 * Time: 10:15
 */

//前台首页获取一条新闻接口

require_once 'db.php';
require_once 'response.php';

header('content-type:text/html:charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

try{
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$sql = "SELECT news_id,news_title,news_content,news_time,news_maker FROM news ORDER BY news_id DESC LIMIT 1";

$res = $connect->query($sql);

$data = array();

if($res && $res->num_rows>0) {
    while($info = $res->fetch_assoc()) {
        $data[] = $info;
    }
    return Response::show(200,'首页新闻数据获取成功',$data);
} else {
    return Response::show(404,'首页新闻数据获取失败');
}